<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}

require_once('adminApp.php');

class SysmenuController extends AdminAppController {
	
	public function __construct() {
		$this->chkLoginType='html';
        parent::__construct();
        $this->menuLogic = $this->Logic('MenuLogic');
	}
 	
 	public function actionDefault(){        
        $res = array();
        try{
            $topmenu =$this->menuLogic->getMenuAll("*", "parent_id=0 ", "order by ORDER_NUM asc");
            $datamenu =$this->menuLogic->getMenuAll("*", "parent_id!=0", "order by ORDER_NUM asc");
        }catch(Exception $e){
            echo $e->getMessage();
        }
        T::$tpl->set("topmenu", $topmenu);
        T::$tpl->set("datamenu", $datamenu);
 	}
    /** 添加菜单 */
    public function actionAdd(){
        T::$tplFile = "admin/sysmenu/act";
        $mainMenus = $this->menuLogic->getMenuAll('*','STATE=1 AND parent_id=0','Order by order_num');
        T::$tpl->set("mainMenus", $mainMenus);
        if($_POST['actname']=='save'){
            $this->SaveData($_POST, 'add');
        }       
    }
    /** 修改菜单 */
    public function actionEdit(){
        T::$tplFile = "admin/sysmenu/act";         
        $id = $_REQUEST['id'];
        if($id){            
            if($_POST['actname']=='save'){
                $this->SaveData($_POST, 'edit');
            }else{
                $mainMenus = $this->menuLogic->getMenuAll('*','STATE=1 AND parent_id=0','Order by order_num');
                T::$tpl->set("mainMenus", $mainMenus);
                //显示
                $menuItem = $this->menuLogic->getMenuItem("*","ID=".$id);
                T::$tpl->set('menuItem', $menuItem);
            }
        }else{
            $this->sysMessage("修改失败,要修改的ID异常");
        }       
    }   
    /** 保存添加、修改 */
    private function SaveData($data, $type){
        if($type=='add'){
            $res = $this->menuLogic->addMenuItem($data);
            if($res){
                $this->sysMessage("添加成功");
            }else{
                $this->sysMessage("添加失败,请重试");
            }
        }elseif($type=='edit'){
            $res = $this->menuLogic->updateMenuItem($data, "ID=".$data['id']);
            if($res){
                $this->sysMessage("修改成功");
            }else{
                $this->sysMessage("修改失败,请重试");
            }
        }else{
            $this->sysMessage("未知操作..");
        }
    }   
    /** 删除菜单 */
    public function actionDel(){
       T::$tplFile='public/empty';
        $id = intval($_REQUEST['id']);
        if($id){
            $menuItem = $this->menuLogic->getMenuAll("*","PARENT_ID=".$id);
            foreach ($menuItem as $key => $value) {
                $this->menuLogic->delFunsItem("MENU_ID=".$value['ID']);  
            }
            $this->menuLogic->delMenuItem("ID=".$id);
            $this->menuLogic->delMenuItem("PARENT_ID=".$id);
            $this->menuLogic->delFunsItem("MENU_ID=".$id);  
            return $this->sysMessage("操作成功",array("返回菜单页"=>"admin.php?ctl=sysmenu"), 3);
        }else{
            $this->sysMessage("删除失败,ID异常");
        }
    }   
    /** 菜单功能 */
    public function actionFuns(){
        T::$tplFile = "admin/sysmenu/funs";
        $id = intval($_REQUEST['id']);
        if($id){ 

            try{
                $res =$this->menuLogic->getMenuFuncAll("*", "MENU_ID=".$id, "order by ORDER_NUM asc");
            }catch(Exception $e){
                echo $e->getMessage();
            }
            T::$tpl->set("menuid", $id);
            T::$tpl->set("res", $res);
        }else{
            $this->sysMessage("删除失败,ID异常");
        }
    } 
    /** 添加功能 */
    public function actionFunsAdd(){
        T::$tplFile = "admin/sysmenu/funact";
        $menuid = intval($_REQUEST['id']);
        $mainMenus = $this->menuLogic->getMenuAll('*','STATE=1 AND parent_id!=0','Order by order_num');
        $menuItem['MENU_ID'] = $menuid;
        T::$tpl->set("mainMenus", $mainMenus);
        T::$tpl->set('menuItem', $menuItem);
        if($_POST['actname']=='save'){
            $this->FunsSaveData($_POST, 'add');
        }       
    }
    /** 修改功能 */
    public function actionFunsEdit(){
        T::$tplFile = "admin/sysmenu/funact";         
        $id = $_REQUEST['id'];
        if($id){            
            if($_POST['actname']=='save'){
                $this->FunsSaveData($_POST, 'edit');
            }else{
                $mainMenus = $this->menuLogic->getMenuAll('*','STATE=1 AND parent_id!=0','Order by order_num');
                T::$tpl->set("mainMenus", $mainMenus);
                //显示
                $menuItem = $this->menuLogic->getMenuFunItem("*","ID=".$id);
                T::$tpl->set('menuItem', $menuItem);
            }
        }else{
            $this->sysMessage("修改失败,要修改的ID异常");
        }       
    }   
    /** 保存添加、修改 */
    private function FunsSaveData($data, $type){
        if($type=='add'){
            $res = $this->menuLogic->addFunsItem($data);
            if($res){
                $this->sysMessage("添加成功");
            }else{
                $this->sysMessage("添加失败,请重试");
            }
        }elseif($type=='edit'){
            $res = $this->menuLogic->updateFunsItem($data, "ID=".$data['id']);
            if($res){
                $this->sysMessage("修改成功");
            }else{
                $this->sysMessage("修改失败,请重试");
            }
        }else{
            $this->sysMessage("未知操作..");
        }
    } 
    /** 删除菜单 */
    public function actionFunsDel(){
        T::$tplFile='public/empty';
        $id = intval($_REQUEST['id']);
        $menuid = intval($_REQUEST['menuid']);
        if($id){            
            $menuItem = $this->menuLogic->delFunsItem("ID=".$id);
            return $this->sysMessage("操作成功",array("返回菜单页"=>"admin.php?ctl=sysmenu&act=funs&id=".$menuid), 3);
        }else{
            $this->sysMessage("删除失败,ID异常");
        }
    }  
}//End Class